<?php
namespace tool;

class Utils{

    public static function curl($url, $data, array $header = [], $method = 'post'){
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在 // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer

        if (strtolower($method) == 'post') {
            curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        }

        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        if (!empty($header)){
            //默认x-www-form-urlencoded
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        }
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据
    }


    public static function getAge($birthday){
        $byear=date('Y',$birthday);
        $bmonth=date('m',$birthday);
        $bday=date('d',$birthday);

        $tyear=date('Y');
        $tmonth=date('m');
        $tday=date('d');

        $age=$tyear-$byear;
        if($bmonth>$tmonth || $bmonth==$tmonth && $bday>$tday){
            $age--;
        }
        return $age;
    }

    /**
     * 获取当前季度202001
     */
    public static function getQuarter(){
        $start_date = '2020-10-01';
        $end_date = date('Y-m-d');
        if ($start_date < $end_date){
            return 1;
        }
        $start = new \DateTime($start_date);
        $end =  new \DateTime($end_date);
        $diff = $start->diff($end);
        $diff_month = $diff->format('%y')*12+$diff->format('%m');
        $num = ceil($diff_month / 3);
        return $num ? $num: 1;
    }


    public static function getMonthNum($start_date, $end_date, $tags='-' ){
        $date1 = explode($tags,$start_date);
        $date2 = explode($tags,$end_date);
        return abs($date2[0] - $date1[0]) * 12 + abs($date2[1] - $date1[1]);
    }


    public static function str_to_ascii($str = ''){
        $arr = str_split($str);
        $result = '';
        foreach ($arr as $s){
            $result .= bin2hex($s);
        }
        return $result;
    }

    public static function ascii_to_str($str = ''){
        $arr = str_split($str, 2);
        $result = '';
        foreach ($arr as $s){
            $result .=hex2bin($s);
        }
        return $result;
    }

    //获取真实IP
    public static function get_real_ip(){
        $ip = '';
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos    =   array_search('unknown',$arr);
            if(false !== $pos) unset($arr[$pos]);
            $ip     =   trim($arr[0]);
        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip     =   $_SERVER['HTTP_CLIENT_IP'];
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip     =   $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
}